<?php
class FacilityController extends CBaseController
{
	protected $facility;
	protected $validator;
	
	public function __construct($config=null) 
    { 
      parent::__construct($config);	  
    } 
	
	public function init($tpl)
	{
		parent::init($tpl);
		$this->reqLogin();
		$this->templator->addViewsDir(dirname(__FILE__).'/../views');
		$this->prepare();
	}	
	protected function prepare()
	{
		if(isset($_GET['id']) && !empty($_GET['id']))
			$this->facility = R::load('facility',$_GET['id']);
		elseif(isset($_POST['id']) && !empty($_POST['id']))
			$this->facility = R::load('facility',$_POST['id']);		
		else
			$this->facility = R::dispense('facility');		
	}
	public function all()
	{
	 $facilitys = R::findAll('facility',' ORDER BY name');
	 $this->templator->assign('facilitys',array_merge($facilitys, array()));
	 $this->templator->display('facility_list.html');
	}
	
	public function savePrepare()
	{
		$this->facility->name = $_POST['name'];
		$this->facility->facilitycode= $_POST['facilitycode'];
		$this->facility->icon= $_POST['icon'];
	}
	
	public function updatePrepare()
	{
		$this->facility->name = $_POST['name'];		
		$this->facility->facilitycode = $_POST['facilitycode'];		
		$this->facility->icon = $_POST['icon'];		
	}
	
	public function saveORupdateValidate()
	{
		$this->validator = new FormValidator();
		$this->validator->addValidation("name","req","Please fill in Name");
		return $this->validator->ValidateForm();
	}
		
	
	public function create()
	{
		$this->templator->assignByRef('facility',$this->facility);
		$this->templator->display('facility_general.html');
	}
	public function save()
	{
	  $this->savePrepare();
	  if($this->saveORupdateValidate())
	  {
		R::store($this->facility);
		$this->templator->assign('info','Facility added sucessfully');
		$this->templator->assignByRef('facility',$this->facility);		
		$this->templator->display('facility_general.html');	
	  }
	  else
	  {
		$error_hash = $this->validator->GetErrors();
		$this->templator->assign('validate_errors',$error_hash);
		$this->templator->assignByRef('facility',$this->facility);
		$this->templator->display('facility_general.html');
	  }
	}
	
	public function edit()
	{
		$this->templator->assignByRef('facility',$this->facility);		
		$this->templator->display('facility_general.html');
	}
	
	public function update()
	{
		$this->updatePrepare();
		  if($this->saveORupdateValidate())
		  {
			R::store($this->facility);
			$this->templator->assignByRef('facility',$this->facility);
			$this->templator->assign('info','Facility updated sucessfully');
			$this->templator->display('facility_general.html');	
		  }
		  else
		  {
			$error_hash = $this->validator->GetErrors();
			$this->templator->assign('validate_errors',$error_hash);
			$this->templator->assignByRef('facility',$this->facility);
			$this->templator->display('facility_general.html');
		  }
	}
	
	public function delete()
	{
		if(isset($this->facility))
		{
			R::trash($this->facility);
			$this->templator->assign('info','Facility deleted sucessfully');
		}
		$this->all();
	}
}	